package com.yjd.comm.demo;

import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 修改配置文件
 * 
 * @author pc
 * 
 */
public class TestPropertiesModify {

	static String test = "test1117-";
//	static String phptest = "-10130";
	static String phptest = "test1117-";
	static String testPfPath = "D:\\workspace_branch\\y_parent\\config\\test.properties";

	@SuppressWarnings("unchecked")
	static Map<String, String> map = FrameUtil.newHashMap();
	static {
//		map.put("https://www.yijiedai.org", "https://www" + phptest
//				+ ".yijiedai.org");
//		map.put("https://user.yijiedai.org", "https://user" + phptest
//				+ ".yijiedai.org");
		map.put("https://www.yijiedai.org", "https://" + phptest
				+ "www.yijiedai.org");
		map.put("https://user.yijiedai.org", "https://" + phptest
				+ "user.yijiedai.org");
		map.put("http://static.yijiedai.org", "http://" + test
				+ "static.yijiedai.org");
		map.put("https://static.yijiedai.org", "https://" + test
				+ "static.yijiedai.org");
		map.put("http://mfq.yijiedai.org", "http://" + test
				+ "mfq.yijiedai.org");
		map.put("http://admina-act.yijiedai.org", "http://" + test
				+ "admina-act.yijiedai.org");
		map.put("http://lck.yijiedai.org", "http://" + test
				+ "lck.yijiedai.org");
		map.put("https://yxb.yijiedai.org", "https://" + test
				+ "yxb.yijiedai.org");
		map.put("http://openapi.yijiedai.org", "http://" + test
				+ "openapi.yijiedai.org");
		map.put("http://admina-openapi.yijiedai.org", "http://" + test
				+ "admina-openapi.yijiedai.org");
		map.put("http://admina-debt.yijiedai.org", "http://" + test
				+ "admina-debt.yijiedai.org");
		for (Map.Entry<String, String> entry : map.entrySet()) {
			System.out.println(String.format(
					"<a target='_blank' href='%s'>%s</a><br/><br/>",
					entry.getValue(), entry.getValue()));
		}
	}

	public static void main(String[] args) {
		File file = new File(testPfPath);
		List<String> strings = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF-8"));
			String s = null;
			while ((s = reader.readLine()) != null) {
				strings.add(replace(s));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}

		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file), "UTF-8"));
			file.createNewFile();
			int i = 1;
			for (String string : strings) {
				writer.write(string);
				if (i < strings.size()) {
					writer.newLine();
				}
				i++;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (writer != null) {
				try {
					writer.flush();
					writer.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
		System.out.println("ok");
	}

	public static String replace(String s) {
		if (StringUtil.isNotEmpty(s)) {
			for (Map.Entry<String, String> entry : map.entrySet()) {
				s = s.replaceAll(entry.getKey(), entry.getValue());
			}
		}
		return s;
	}
}
